package com.cmy.framework_base.basics

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

/**
 * @name:
 *  BasicsLazyFragment
 * @package:
 *  com.cmy.framework_base.basics
 * @date:
 *  2023/4/711:52
 * @author
 *    cmy
 * @description
 *
 */
abstract class BasicsLazyFragment : BasicsFragment(){



    /**
     * 标志位，标志已经初始化完成。
     */
    @JvmField
    protected var isPrepared = false


    /**
     * 标志位，是否需要加载数据
     */
    @JvmField
    protected var isNeedLoadData = true


    /**
     * 懒加载的显示处理
     */
    @JvmField
    protected var isToUserVisible: Boolean = false

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        isPrepared = true


        return super.onCreateView(inflater, container, savedInstanceState)
    }
    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden) {
            isToUserVisible = true
            onVisible()
        } else {
            isToUserVisible = false
            onInvisible()
        }
    }




    override fun onResume() {
        super.onResume()
       initData()
    }


    override fun onDestroyView() {
        super.onDestroyView()
        isPrepared = false
    }


    override fun initData() {
        if (!isHidden) {
            isToUserVisible = true
            onVisible()
        }
    }

    open fun onVisible() {
        if (isPrepared && isToUserVisible && isNeedLoadData) {
            isNeedLoadData = false
            lazyLoad()
        }
    }

    open fun onInvisible() {}

    protected abstract fun lazyLoad()


}